package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminCompanyDao;
import ex.com.jm.food.entity.Company;

/**
 * dao层实现层
 * @author zhaojw
 *
 */
public class AdminCompanyDaoImpl extends HibernateDaoSupport implements
		AdminCompanyDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Company> getCompanies(final int initPage,final int pageRow,final String name,
			final Integer status,final String s_date,final String e_date,final String sort,
			final String order) {
		// TODO Auto-generated method stub
		Object companies=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.CompanyDTO(com.comId,com.comName,com.comEmail,"
						+ "com.created,com.status,com.profit,"
						+ "com.comAddress,com.comPhone,com.comUser) from Company com where 1=1  ";
				if(name!=null&&!name.equals("")){
					hql +=" and com.comName like '%"+name+"%' ";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql += " and com.created >= '"+s_date+"'";
				}
				if(e_date !=null && e_date.equals("")){
					hql += " and com.created <='"+e_date+"'";
				}
				if(status!=null && !status.equals("")){
					hql += " and com.status ="+status;
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql += " order by com."+order+" "+sort;
				}else {
					hql +=" order by com.created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Company>) companies;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int companyCount(final String name,final Integer status,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Company com where 1=1  ";
				if(name!=null&&!name.equals("")){
					hql +=" and com.comName like '%"+name+"%' ";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql += " and com.created >= '"+s_date+"'";
				}
				if(e_date !=null && e_date.equals("")){
					hql += " and com.created <='"+e_date+"'";
				}
				if(status!=null && !status.equals("")){
					hql += " and com.status ="+status;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		
		return count;
	}

	@Override
	public Object saveCompany(Company company) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(company);
	}

	@Override
	public void updateCompany(Company company) {
		// TODO Auto-generated method stub
           this.getHibernateTemplate().update(company);
	}

	@Override
	public Company editCompany(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("update Company set status="+status+" where comId='"+id+"'");
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}else{
		return false;
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updatePower(final String power,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("update Company set comPower="+power+" where comId='"+id+"'");
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}else{
		return false;
		}
	}

	@Override
	public Company intoAccount(String id, String name) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Company.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Company checkName(final String name) {
		// TODO Auto-generated method stub
		Company company=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from Company where comName='"+name+"'");
				return query.uniqueResult();
			}
		});
		return company;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Company checkEmail(final String email) {
		// TODO Auto-generated method stub
		Company company=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from Company where comEmail='"+email+"'");
				return query.uniqueResult();
			}
		});
		return company;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public String getPower(final String id) {
		// TODO Auto-generated method stub
		String power=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("select comPower from Company where comId='"+id+"'");
				return query.uniqueResult();
			}
		});
		return power;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Company checkBankCard(final String card) {
		// TODO Auto-generated method stub
		Company bandCard=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from Company where bankCard='"+card+"'");
				return query.uniqueResult();
			}
		});
		return bandCard;
	}

}
